<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>4-2 父子组件间的数据传递</title>
    <script src="../vue.js"></script>
</head>

<body>
    <div id="root">
        <counter1 :count="3" @inc="handleIncrease"></counter1>
        <counter1 :count="2" @inc="handleIncrease"></counter1>
        <div>{{total}}</div>
    </div>

    <script>
        // Vue采用的是单向数据流，也就是说，数据是从父组件流向子组件，子组件不能直接修改父组件的数据。
        // 子组件可以通过$emit方法向父组件发送事件。父组件可以在子组件上使用v-on来监听事件 
        var counter = {
            props: ['count'],
            data() {
                return {
                    number: this.count
                }
            },
            template: '<div @click="handleClick">{{number}}</div>',
            methods: {
                handleClick() {
                    this.number = this.number + 2;
                    this.$emit('inc', 2);
                }
            },
        }

        var vm = new Vue({
            el: '#root',
            data: {
                total: 5
            },
            components: {
                counter1: counter
            },
            methods: {
                handleIncrease(step) {
                    this.total += step;
                }
            }
        })
    </script>
</body>

</html>